package com.team.cluedo.app.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.team.cluedo.R;
import com.team.cluedo.app.PlayerFiche;
import com.team.cluedo.fc.Card;

public class ImageAdaptater extends BaseAdapter {
   
	int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds;
    
    private int width, height;
    
    private static final int DEFAULT_WIDTH = 150;
    private static final int DEFAULT_HEIGHT = 200;

  /*  public ImageAdaptater(Context c) {
        mContext = c;
//        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
//        mGalleryItemBackground = a.getResourceId(
//                android.R.styleable.Theme_galleryItemBackground, 0);
//        a.recycle();
    }*/

    public ImageAdaptater(Context c,String[] cartes) {
        mContext = c;
        mImageIds = toImageAdapter(cartes);
        this.width = DEFAULT_WIDTH;
        this.height = DEFAULT_HEIGHT;
//        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
//        mGalleryItemBackground = a.getResourceId(
//                android.R.styleable.Gallery1_android_galleryItemBackground, 0);
//        a.recycle();
    }
    public ImageAdaptater(PlayerFiche c, Card[] cartes, int width, int height) {
   	 	mContext = c;
        mImageIds = toImageAdapter(cartes);
        this.width = width;
        this.height = height;
        
	}
    
    
    public ImageAdaptater(Context c,String[] cartes, int width, int height) {
        mContext = c;
        mImageIds = toImageAdapter(cartes);
        this.width = width;
        this.height = height;
        
//        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
//        mGalleryItemBackground = a.getResourceId(
//                android.R.styleable.Theme_galleryItemBackground, 0);
//        a.recycle();
    }
    
    private Integer[] toImageAdapter(String[] cartes){
    	Integer[] result = new Integer[cartes.length];
    	
    	for(int i = 0; i <cartes.length; i++){
    		
    		//Personnage
    		if(cartes[i].equalsIgnoreCase("green")){
    			result[i] = R.drawable.reverendgreen;
    		}
    		else if(cartes[i].equalsIgnoreCase("mustard")){
    			result[i] = R.drawable.colonelmustard;
    		}
    		else if(cartes[i].equalsIgnoreCase("scarlet")){
    			result[i] = (R.drawable.missscarlet);
    		}
    		else if(cartes[i].equalsIgnoreCase("peacock")){
    			result[i] = (R.drawable.mrspeacock);
    		}
    		else if(cartes[i].equalsIgnoreCase("white")){
    			result[i] = (R.drawable.mrswhite);
    		}
    		else if(cartes[i].equalsIgnoreCase("plum")){
    			result[i] = (R.drawable.professorplum);
    		}
    		
    		
    		//Salle"hall","ballroom","conservatory","billard_room","library","study","lounge","dining_room","kitchen"
    		
    		else if(cartes[i].equalsIgnoreCase("hall")){
    			result[i] = R.drawable.hall;
    		}
    		else if(cartes[i].equalsIgnoreCase("ballroom")){
    			result[i] = R.drawable.ballroom;
    		}
    		else if(cartes[i].equalsIgnoreCase("conservatory")){
    			result[i] = (R.drawable.conversatory);
    		}
    		else if(cartes[i].equalsIgnoreCase("billard_room")){
    			result[i] = (R.drawable.billardroom);
    		}
    		else if(cartes[i].equalsIgnoreCase("study")){
    			result[i] = (R.drawable.study);
    		}
    		else if(cartes[i].equalsIgnoreCase("library")){
    			result[i] = (R.drawable.study);
    		}
    		else if(cartes[i].equalsIgnoreCase("lounge")){
    			result[i] = (R.drawable.lounge);
    		}
    		else if(cartes[i].equalsIgnoreCase("dining_room")){
    			result[i] = (R.drawable.diningroom);
    		}
    		else if(cartes[i].equalsIgnoreCase("kitchen")){
    			result[i] = (R.drawable.kitchen);
    		}
    		
    		
    		
    		//arme "candlestick","dagger","lead_pipe","revolver","rope","spanner"
    		
    		else if(cartes[i].equalsIgnoreCase("candlestick")){
    			result[i] = R.drawable.candlestick;
    		}
    		else if(cartes[i].equalsIgnoreCase("dagger")){
    			result[i] = R.drawable.dagger;
    		}
    		else if(cartes[i].equalsIgnoreCase("lead_pipe")){
    			result[i] = (R.drawable.leadpipe);
    		}
    		else if(cartes[i].equalsIgnoreCase("revolver")){
    			result[i] = (R.drawable.revolver);
    		}
    		else if(cartes[i].equalsIgnoreCase("rope")){
    			result[i] = (R.drawable.rope);
    		}
    		else if(cartes[i].equalsIgnoreCase("spanner")){
    			result[i] = (R.drawable.spanner);
    		}
    	}
   
    	return result;
    }
    
    private Integer[] toImageAdapter(Card[] cartes){
    	Integer[] result = new Integer[cartes.length];
    	
    	for(int i=0;i<cartes.length;i++){
    		
    		//Personnage
    		if(cartes[i].getName().equalsIgnoreCase("green")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.reverendgreen;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("mustard")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.colonelmustard;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("scarlet")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.missscarlet);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("peacock")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.mrspeacock);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("white")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.mrswhite);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("plum")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.professorplum);
    			}
    			
    		}
    		
    		
    		//Salle"hall","ballroom","conservatory","billard_room","library","study","lounge","dining_room","kitchen"
    		
    		if(cartes[i].getName().equalsIgnoreCase("hall")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.hall;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("ballroom")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.ballroom;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("conservatory")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.conversatory);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("billard_room")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.billardroom);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("study")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.study);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("library")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.study);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("lounge")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.lounge);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("dining_room")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.diningroom);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("kitchen")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.kitchen);
    			}
    			
    		}
    		
    		
    		
    		//arme "candlestick","dagger","lead_pipe","revolver","rope","spanner"
    		
    		if(cartes[i].getName().equalsIgnoreCase("candlestick")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.candlestick;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("dagger")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = R.drawable.dagger;
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("lead_pipe")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.leadpipe);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("revolver")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.revolver);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("rope")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.rope);
    			}
    			
    		}
    		if(cartes[i].getName().equalsIgnoreCase("spanner")){
    			if(cartes[i].isSelected()){
    				result[i] = R.drawable.cover;
    			}else{
    				result[i] = (R.drawable.spanner);
    			}
    			
    		}
    	}
   
    	return result;
    }
    
    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(width, height));
      //  i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}
